// pages/main/main.js
import {
  HttpApi
} from '../../../model/index.js'
const httpApi = new HttpApi

Page({
  /**
   * 页面的初始数据
   */
  data: {
    keyword: "",
    communityList: [],
    createBy: ""
  },


  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function (options) {
    // this.search()
    var _this = this
    var login = wx.getStorageSync('login')
   
    httpApi.centre().then(res => {
      if (res.code == 0) {
        //获取token
        _this.setData({
          userinfo: res.data,
        })
        console.log("@@" + this.data.userinfo.createBy);
        wx.setStorage({
          key: 'userinfo',
          data: res.data
        })
      }
    })
  },
  bindCodeInput(e) {
    var that = this;
    this.setData({
      keyword: e.detail.value
    })
    if (e.detail.value != ""){
      that.search();
    }else{
      that.setData({
        communityList: [],
      })
    }
    console.log(this.data.keyword)
  },
  search() {
    var _this = this
    httpApi.communityList(this.data.keyword).then(res => {
      // console.log("communityList:",res.data)
      _this.setData({
        communityList: res.data,
      })
    })
  },
  createBy: function (e) {
    console.log("createBy:", e.detail.value)
    this.setData({
      createBy: e.detail.value
    })
  },
  goRoomNumber(e) {
    console.log("goRoomNumber:", e.currentTarget.dataset)
    var communityId = e.currentTarget.dataset.communityid
    wx.navigateTo({
      url: "../u_input_roomNumber/u_input_roomNumber?communityId=" + communityId + "&yuyue=1"
    })
  },
  sub() {
    var _this = this;
    wx.showModal({
      title: '提示',
      content: '确定要预约吗？',
      success: function (sm) {
        if (sm.confirm) {
          httpApi.addOrder(_this.data.createBy).then(res => {
            if (res.code == 0) {
              wx.navigateTo({
                url: "../../home"
              })
              wx.showToast({
                title: "请等候回电"
              })

            } else {
              wx.showToast({
                title: res.msg
              })
            }
          })

        } else if (sm.cancel) {
          wx.navigateTo({
            url: "../../home"
          })
        }
      }
    })
  },
  off() {
    wx.navigateTo({
      url: "../../home"
    })
  },

  toMain() {
    wx.navigateTo({
      url: "../../home"
    })
  },

  onHide() {

  },

  onUnload() {

  },
  /* 接收子组件值 */
  close(e) {
    this.setData({

    })
  }
})
